// pages/mine/winningRecordList/winningRecordList.js
import Common from '../../../common/common.js';
let common = new Common;
const app = getApp();
Page({
  data: {
    pageList: [],
    pageNumber: 1,
    isEnd: false, //是否到底
    orderLxList: [
      { id: 1, name: '商家配送' }, { id: 2, name: '到店自提' }
    ],
    hidden: true,
    orderTypeId: '',//选中订单类型\
    psTypeId: 1,//选中配送方式 1待配送  2已配送
    keyValue: ''// 搜索条件
  },
  onLoad: function (options) {
    this.getPageList(1, 1);
    this.queryDeliverStatus();
  },
  onShow: function () { },
  // 搜索条件
  searchList: function (e) {
    console.log(e)
    this.setData({
      orderTypeId:'',
      psTypeId:'',
      keyValue: e.detail.value
    })
    this.getPageList(1, 1);
  },
  // 确定取消
  hideMask: function (e) {
    if (e.currentTarget.dataset.no == 0) {
      this.setData({
        orderTypeId: '',
      })
    }
    this.setData({
      hidden: true,
    });
    this.getPageList(1, 1);
  },
  // 点击订单类型
  orderType: function (e) {
    let id = e.currentTarget.dataset.id;
    if (this.data.orderTypeId == id) {
      this.setData({
        orderTypeId: '',
      })
    } else {
      this.setData({
        orderTypeId: id
      })
    }
  },
  // 点击订单状态
  product: function (e) {
    let id = e.currentTarget.dataset.id;
    if (this.data.psTypeId != id) {
      this.setData({
        psTypeId: id
      })
    }
    this.getPageList(1, 1)
  },
  // 点击筛选出现弹框
  showMask: function (e) {
    this.setData({
      hidden: false,
    })
  },
  lookClickPs: function (e) {
    let that = this;
    let uuid = e.currentTarget.dataset.item.uuid
    wx.showModal({
      title: "提示",
      content: "确认配送？",
      showCancel: true,
      cancelText: "取消",
      confirmText: "确定",
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          that.sureSubmit(uuid)
        }
      }
    })
  },
  // 确认配送
  sureSubmit: function (uuid) {
    // return
    common.requestAjax('/api/promotion-service/v1/winningRecord/updateDeliverByStore?uuid=' + uuid, 'GET', {}).then(res => {
      if (res.data.code) {
        wx.showToast({
          title: '操作已完成!',
          icon: 'none'
        })
        setTimeout(() => {
          this.getPageList(1, 1);
        }, 2000);
      } else {
        wx.showToast({
          title: '操作失败!',
          icon: 'none'
        })
      }
    })
  },
  comfrimClickPs(e) {
    let that = this;
    let uuid = e.currentTarget.dataset.item.uuid
    wx.showModal({
      title: "提示",
      content: "请确认客户已来门店完成自提?",
      showCancel: true,
      cancelText: "取消",
      confirmText: "确定",
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          // return;
          that.sureSubmit(uuid)
        }
      }
    })
  },

  getPageList(page, type) {
    let data = {
      "size": 10,
      "current": page,
      "ascs": [],
      "descs": [],
      "condition": {
        "orgSeq": app.globalData.orgSeq,
        "memberId": '',
        "keyword": this.data.keyValue,
        "memberLevel": "",
        "beginDate": '',
        "endDate": '',
        'memberPstype': this.data.orderTypeId,//配送方式
        'isReceive': this.data.psTypeId,//配送状态
      }
    }
    common.requestAjax('/api/promotion-service/v1/winningRecord/queryWinningList', 'POST', data).then(res => {
      if (res.data.code) {
        let arraylist = this.data.pageList || [];
        let isEnd = this.data.isEnd;
        if (type == 1) {
          //下拉刷新
          arraylist = res.data.records;
          isEnd = false;
        } else if (type == 2) {
          //上拉加载
          arraylist = arraylist.concat(res.data.records);
          if (res.data.records.length == 0) {
            //最后一行没数据 到底了
            isEnd = true;
          }
          setTimeout(() => {
            wx.hideLoading()
          }, 500)
        }
        this.setData({
          pageList: arraylist,
          isEnd,
        })
      }

    })
  },
  onReachBottom: function () {
    if (!this.data.isEnd) {
      let pageNumber = this.data.pageNumber;
      pageNumber += 1;

      this.getPageList(pageNumber, 2)

      wx.showLoading({
        title: '正在加载中...',
      })
      this.setData({
        pageNumber
      })
    }
  },
  //查询已配送数量
  queryDeliverStatus(){
    common.requestAjax('/api/promotion-service/v1/winningRecord/queryDeliverStatus','post',{orgSeq:app.globalData.orgSeq}).then(res=>{
      if(res.data.code){
        this.setData({
          daiNum:res.data.result.deliverNum,
          yiNum:res.data.result.reciveNum
        })
      }
    })
  },
  lookClick(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `../addressEdit/addressEdit?winUuid=${item.uuid}&type=1`,
    })
  }
})